package com.yenlien.traceyun.common.utils;

import javax.swing.text.DateFormatter;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * @author: bigwangy@sina.com
 * @create: 2020-07-21
 * @description:
 **/
public class DateUtil {

    /**
     * 传入long时间戳 ，转换成格式化的String类型时间
     *
     * @param time
     * @return
     */
    public static String TimeOfLongToStr(long time) {
        DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
    }


    /**
     * 传入String类型格式化时间，转换成Long类型的时间戳
     *
     * @param strTime
     * @return
     */
    public static long timeOfStrToLong(String strTime) {
        DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse(strTime, ftf);
        return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * 传入String类型格式化时间，转换成Long类型的时间戳
     *
     * @param strTime
     * @return
     */
    public static long dayOfStrToLong(String strTime) {
        DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate parse = LocalDate.parse(strTime, ftf);
        return LocalDate.from(parse).atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    public static Date toDay(String day) {
        DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate parse = LocalDate.parse(day, ftf);
        Instant instant = LocalDate.from(parse).atStartOfDay(ZoneId.systemDefault()).toInstant();
        return Date.from(instant);
    }

    public static int getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }

    public static int getDateYear(String date) {
        return getYear(toDay(date));
    }

    public static Date toDaytime(String daytime) {
        DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse(daytime, ftf);
        Instant instant = LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant();
        return Date.from(instant);
    }

    /**
     * 通过LocalDateTime获取当前格式化时间
     *
     * @return
     */
    public static String getDateTime() {
        //DateTimeFormatter替换了SimpleDateFormat, 因为线程安全问题。
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }


    /**
     * 通过LocalDateTime获取当前格式化时间
     *
     * @return
     */
    public static String getDate() {
        //DateTimeFormatter替换了SimpleDateFormat, 因为线程安全问题。
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    /**
     * 通过date
     *
     * @param date
     * @return
     */
    public static String getDateTime(Date date) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        String dateStr = dateTimeFormatter.format(localDateTime);
        return dateStr;
    }

    /**
     * 通过date
     *
     * @param date
     * @return
     */
    public static String getDate(Date date) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        String dateStr = dateTimeFormatter.format(localDateTime);
        return dateStr;
    }

    public static void main(String[] args) {
        System.out.println(dayOfStrToLong("2020/10/29".replaceAll("/", "-")));
    }

}
